#include "burner.h"
#include "version.h"
#include <shellapi.h>
#include "build_details.h"

// Print text surrounded by a black outline
static void myDrawText(HDC hdc, RECT* rect, wchar_t* text, int sizeDelta, int color, UINT format)
{
	wchar_t szName[32];
	RECT textRect;
	TEXTMETRIC myMetric;

	GetTextFace(hdc, 32, szName);
	GetTextMetrics(hdc, &myMetric);
	HFONT myFont = CreateFont(myMetric.tmHeight + sizeDelta, 0, 0, 0, FW_BLACK, 0, 0, 0, 0, 0, 0, ANTIALIASED_QUALITY, 0, szName);
	HFONT hFont = (HFONT)SelectObject(hdc, myFont);
	SetBkMode(hdc, TRANSPARENT);

	// Shrink the textRect to allow space for the outline
	memcpy(&textRect, rect, sizeof(RECT));
	textRect.left += 1;
	textRect.top += 1;
	textRect.right -= 1;
	textRect.bottom -= 1;

	// Print the outline first
	SetTextColor(hdc, 0);
	textRect.left -= 1;
	textRect.right -= 1;
	DrawText(hdc, text, -1, &textRect, format);
	textRect.top -= 1;
	textRect.bottom -= 1;
	DrawText(hdc, text, -1, &textRect, format);
	textRect.left += 1;
	textRect.right += 1;
	DrawText(hdc, text, -1, &textRect, format);
	textRect.left += 1;
	textRect.right += 1;
	DrawText(hdc, text, -1, &textRect, format);
	textRect.top += 1;
	textRect.bottom += 1;
	DrawText(hdc, text, -1, &textRect, format);
	textRect.top += 1;
	textRect.bottom += 1;
	DrawText(hdc, text, -1, &textRect, format);
	textRect.left -= 1;
	textRect.right -= 1;
	DrawText(hdc, text, -1, &textRect, format);
	textRect.left -= 1;
	textRect.right -= 1;
	DrawText(hdc, text, -1, &textRect, format);
	textRect.left += 1;
	textRect.right += 1;
	textRect.top -= 1;
	textRect.bottom -= 1;

	// Then print the text on top
	SetTextColor(hdc, color);
	DrawText(hdc, text, -1, &textRect, format);

	SelectObject(hdc, hFont);
	DeleteObject(myFont);
}

static bool aboutDrawStrings(unsigned int controlID, LPDRAWITEMSTRUCT pdis)
{
	switch (controlID) {
		case IDC_FBA_VER: {
			wchar_t szVerString[128];
			if ((burnVersion & 0xFFFF) > 0x9990) {

				int nVer1 = ((burnVersion >> 16) & 0xFF) + 1;
				if ((nVer1 & 0x0F) > 9) {
					nVer1 += 6;
				}
				int nVer2 = nVer1 & 0x0F;
				nVer1 >>= 4;

				swprintf(szVerString, APP_TITLE L" v%s (v%i.%i Release Candidate %i)", appBurnVer, nVer1, nVer2, burnVersion & 0x0F);
			} else {
				if (burnVersion & 0x00FF) {
					swprintf(szVerString, APP_TITLE L" v%s (alpha version)", appBurnVer);
				} else {
					if (burnVersion & 0xFF00) {
						swprintf(szVerString, APP_TITLE L" v%s (beta version)", appBurnVer);
					} else {
						swprintf(szVerString, APP_TITLE L" v%s (release version)", appBurnVer);
					}
				}
			}

			myDrawText(pdis->hDC, &pdis->rcItem, szVerString, 2, RGB(0xFF, 0xF7, 0xDF), DT_CENTER);

			return true;
		}
		case IDC_SPECIALSTRING: {
			wchar_t szBuild[MAX_PATH];
			RECT textRect = pdis->rcItem;
			textRect.top += 1;
			swprintf(szBuild, L"built on %hs (%hs)", MAKE_STRING(BUILD_DATE), MAKE_STRING(BUILD_COMP));
			myDrawText(pdis->hDC, &textRect, szBuild, -2, RGB(0xDB, 0xDB, 0xDB), DT_CENTER);
			return true;
		}
	}

	return false;
}

// ----------------------------------------------------------------------------

static INT_PTR CALLBACK aboutProc(HWND hDlg ,UINT msg, WPARAM wParam, LPARAM lParam)
{
	if (msg == WM_INITDIALOG) {
		// ==> load custom resource, added by regret
		HBITMAP hBitmap = (HBITMAP)LoadImage(hAppInst, L"res\\about.bmp", IMAGE_BITMAP, 512, 128, LR_LOADFROMFILE);
		if (!hBitmap) {
			hBitmap = LoadBitmap(hAppInst, MAKEINTRESOURCE(BMP_ABOUT));
		}
		SendDlgItemMessage(hDlg, IDC_ABOUT, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hBitmap);
		// <== load custom resource

		wndInMid(hDlg, hScrnWnd);
		return TRUE;
	}
	else if (msg == WM_DRAWITEM) {
		if (aboutDrawStrings(wParam, (LPDRAWITEMSTRUCT)lParam)) {
			return TRUE;
		}

		return 0;
	}
	else if (msg == WM_NOTIFY) {
		LPNMHDR pnmhdr = (LPNMHDR)lParam;
		switch (pnmhdr->code) {
			case NM_CLICK:
			case NM_RETURN: {
				if (pnmhdr->idFrom == IDC_HOMEPAGE) {
					ShellExecute(NULL, L"open", L"http://code.google.com/p/fbashuffle/", NULL, NULL, SW_SHOWNORMAL);
				}
			}
            break;
		}
		return 0;
	}
	else if (msg == WM_CLOSE) {
		EndDialog(hDlg, 0);
	}
	else if (msg == WM_COMMAND) {
		if (HIWORD(wParam) == BN_CLICKED && (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)) {
			SendMessage(hDlg, WM_CLOSE, 0, 0);
		}
	}

	return 0;
}

int aboutCreate(HWND parent)
{
	localiseDialogBox(IDD_ABOUT, parent, (DLGPROC)aboutProc);
	return 0;
}
